package com.fulihui.weixinsdk.request;

import com.alibaba.fastjson.JSON;
import com.fulihui.weixinsdk.http.HttpMethodEnum;
import com.fulihui.weixinsdk.result.ApiAuthorizerTokenResult;
import com.google.gson.Gson;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.TreeMap;

/**
 * 通过authorizer_refresh_token来刷新公众号的接口调用凭据
 * Created by LeeSon on 2017/3/30 0030.
 */
@Data
public class ApiAuthorizerTokenRequest extends WeixinRequest<ApiAuthorizerTokenResult> {
    private String           component_appid;
    private String           authorizer_appid;
    private String           authorizer_refresh_token;

    private transient String component_access_token;

    @Override
    public Map<String, String> urlParam() {
        Map<String, String> param = new TreeMap<>();
        if (StringUtils.isNotBlank(component_access_token)) {
            param.put("component_access_token", component_access_token);
        }
        return param;
    }

    @Override
    protected void childParam() {

    }

    @Override
    public String service() {
        return "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token";
    }

    @Override
    public String urlEndStr() {
        return null;
    }

    @Override
    public ApiAuthorizerTokenResult parseResult(String respStr) {
        return new Gson().fromJson(respStr, ApiAuthorizerTokenResult.class);
    }

    @Override
    public HttpMethodEnum httpMethod() {
        return HttpMethodEnum.POST;
    }

    @Override
    public String requestData() {
        return JSON.toJSONString(this);
    }
}
